<template>
  <div class="article-page">
    <van-nav-bar fixed placeholder>
      <template #left>
        <van-tabs
          color="#000"
          title-inactive-color="#aaa"
          line-width="12px"
          v-model="active"
        >
          <van-tab title="推荐"></van-tab>
          <van-tab title="最新"></van-tab>
        </van-tabs>
      </template>
      <template #right>
        <div class="logo">
          <img src="@/assets/logo.dbd40807.png" alt="" />
        </div>
      </template>
    </van-nav-bar>
    <van-list @load="onload" v-model="loading">
      <article-from
        v-for="item in list"
        :key="item.id"
        :item="item"
      ></article-from>
    </van-list>
  </div>
</template>

<script>
import { articleAPI } from "@/api/user";
import ArticleFrom from "@/components/ArticleFrom.vue";

export default {
  components: { ArticleFrom },
  name: "article-page",
  data() {
    return {
      loading: false,
      list: [],
      current: 1,
      pageTotal: 1,
      active: 0,
    };
  },
  methods: {
    // 监听load 事件  请求更多数据
    async onload() {
      const res = await articleAPI({
        current: this.current,
        //条件传参
        sorter: this.active ? "" : "weight_desc",
      });
      this.current++;
      this.pageTotal = res.data.pageTotal;
      // console.log(res);
      //list要保留旧数据  追加新数据
      this.list.push(...res.data.rows);
      //请求成功后  loading改为false
      this.loading = false;
    },
  },
  watch: {
    active() {
      this.list = [];
      this.current = 1;
      this.pageTotal = 1;
      this.loading = true;
      this.onload();
    },
  },
};
</script>

<style lang="less" scoped>
.article-page {
  .logo {
    flex: 1;
    display: flex;
    justify-content: flex-end;
    > img {
      width: 64px;
      height: 28px;
      display: block;
      margin-right: 10px;
    }
  }
}
</style>